package com.pospi.pospihold.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.GoodsDto;

import java.util.List;

/**
 * Created by Qiyan on 2016/6/20.
 */
public class SaleRankingAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsDto> goodsDtos;

    public SaleRankingAdapter(Context context, List<GoodsDto> goodsDtos) {
        this.context = context;
        this.goodsDtos = goodsDtos;
    }

    @Override
    public int getCount() {
        return goodsDtos.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsDtos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.sale_ranking_adapter_item, null);
            holder.order = (TextView) convertView.findViewById(R.id.sale_ranking_posi);
            holder.no = (TextView) convertView.findViewById(R.id.sale_ranking_no);
            holder.name = (TextView) convertView.findViewById(R.id.sale_ranking_name);
            holder.price = (TextView) convertView.findViewById(R.id.sale_ranking_price);
            holder.num = (TextView) convertView.findViewById(R.id.sale_ranking_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.order.setText(String.valueOf(position + 1));
        holder.no.setText(goodsDtos.get(position).getCode());
        holder.name.setText(goodsDtos.get(position).getName());
        holder.price.setText(String.valueOf(goodsDtos.get(position).getPrice()));
        holder.num.setText(String.valueOf(goodsDtos.get(position).getNum()));

        return convertView;
    }

    class ViewHolder {
        TextView order;
        TextView no;
        TextView name;
        TextView num;
        TextView price;
    }
}
